<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center',
      }"
    >
      <el-button @click="closeDrawer">关 闭</el-button>
      <multiuse-button type="primary" v-show="type != 'show'" :click="saveHandle">保 存</multiuse-button>
    </div>

    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer :type="'type'" :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { addOne, editOne, getDetailById, getFiAccountType } from '@/api/omFinanceP/MoneyManage/BankInfoMaintain';
import SearchDealer from './SearchDealer';
import { phoneAndSolidify, numberCheck, doubleCheck,excludeZeroDouble } from '@/utils/validate.js';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: {},
      isDealer: false,
      handleData: [],
      detialData: {},
    };
  },
  components: {
    SearchDealer,
  },
  mounted() {
    this.init();
  },
  created() {
    this.accountType();
  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getInfoById();
      }
      if (this.type == 'show') {
        this.formList.map((x) => {
          x.placeholder = '';
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
      }
      if (this.type == 'add') {
        this.formList.forEach((item) => {
          if (item.fieldName === 'dealerCode') {
            item.disabled = false;
          }
        });
      }
    },
    // 资金类型 getFiAccountType
    async accountType(val) {
      const res = await getFiAccountType({ ignoreAccountTypes: [60361001, 60361002], accountType: 60051001 });
      if (res.resultCode === 200) {
        res.data.map((x) => {
          this.formList.find((item) => item.fieldName == 'accountTypeId').itemList.push({ text: x.accountTypeName, value: x.id });
        });
      }
    },
    // 详细
    async getInfoById() {
      const res = await getDetailById(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.forEach((x) => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList.find((x) => x.fieldName == 'cityId').itemList = this.createcityList(res.data.provinceId);
        this.formList.find((x) => x.fieldName == 'districtId').itemList = this.district(res.data.cityId);
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },

    async saveHandle() {
      this.$refs.formPanel.SUBMIT_FORM();

      if (this.saveData != null) {
        let saveData = Object.assign({}, this.rowData, this.detialData, this.saveData);
        saveData.dealerId = this.handleData.length > 0 ? this.handleData[0].dealerAttachId : this.rowData.dealerId;

        let res = {};
        if (this.type == 'add') {
          res = await addOne(saveData);
        }
        if (this.type == 'edit') {
          res = await editOne(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },
    seacherFun() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      console.log('data', data);
      this.handleData = data;

      this.isDealer = val;
      if (data && data.length > 0) {
        this.detialData = { ...data[0] };
        this.formList.forEach((x) => {
          if (data[0][x.fieldName]) {
            x.initialValue = data[0][x.fieldName];
          }
          if (x.fieldName === 'dealerAddress') {
            x.initialValue = data[0]['showRoomAddress'];
          }
        });
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>;
          },
          change: (val) => {
            if (!val) {
              this.formList.forEach((item) => {
                if ((this.handleData.length > 0 && this.handleData[0][item.fieldName]) || item.fieldName === 'dealerAddress') {
                  item.initialValue = null;
                }
              });
            }
          },
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '经销商展厅地址',
          fieldName: 'dealerAddress',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '所属区域',
          fieldName: 'areaName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '所属区域ID',
          fieldName: 'areaId',
          placeholder: '',
          hidden: true,
        },
        // {
        //   type: 'SELECT',
        //   label: '银行名称',
        //   fieldName: 'bankName',
        //   placeholder: '请输入',
        //   itemList: this.createDictList('6094'),
        //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        // },
        {
          type: 'INPUT',
          label: '监管公司',
          fieldName: 'superviseCompany',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '监管人',
          fieldName: 'supervisor',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '监管人电话',
          fieldName: 'supervisorPhone',
          placeholder: '请输入',
          maxlength: 13,
          rules: [
            { required: true, message: '请输入', trigger: 'blur' },
            {
              validator: phoneAndSolidify,
              trigger: 'blur',
            },
          ],
        },
        {
          type: 'INPUT_NUMBER',
          label: '授信额度',
          fieldName: 'creditAmount',
          placeholder: '请输入',
          maxlength: 10,
          rules: [
            {
              validator: excludeZeroDouble,
              trigger: 'blur',
            },
            { required: true, message: '请输入', trigger: 'change' },
          ],
        },
        {
          type: 'DATE',
          label: '授信到期日期',
          fieldName: 'creditExpireDate',
          minDateTime: moment().add(-1, 'days'),
          dateType: 'exactdate',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '保证金类型',
          fieldName: 'depositType',
          placeholder: '请选择',
          itemList: this.createDictList('2152'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '保证金比例(%)',
          fieldName: 'depositRate',
          placeholder: '请输入',
          max: 100,
          min: 1,
          maxlength: 3,
          rules: [
            {
              validator: numberCheck,
              trigger: 'blur',
            },
            { required: true, message: '请输入', trigger: 'change' },
          ],
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountTypeId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        // {
        //   type: 'SELECT',
        //   label: '状态',
        //   fieldName: 'status',
        //   // offsetRightCols: 1,
        //   initialValue: this.type === 'add' ? 10031001 : null,
        //   disabled: this.type === 'add',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1003'),
        //   rules: [{ required: true, message: '请选择', trigger: 'change' }],
        // },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createRegionsList(),
          change: (p) => {
            if (p) {
              this.formList.map((x) => {
                if (x.fieldName == 'cityId') {
                  x.initialValue = '';
                  x.itemList = this.createcityList(p);
                }
              });
            } else {
              this.formList.map((x) => {
                if (x.fieldName == 'cityId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityName',
          placeholder: '请选择',
          disabled: true,
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: (p) => {
            if (p) {
              this.formList.map((x) => {
                if (x.fieldName == 'districtId') {
                  x.initialValue = '';
                  x.itemList = this.district(p);
                }
              });
            } else {
              this.formList.map((x) => {
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          },
        },
        // {
        //   type: 'SELECT',
        //   label: '区县',
        //   fieldName: 'districtId',
        //   placeholder: '请选择',
        //   itemList: [],
        //   rules: [{ required: true, message: '请选择', trigger: 'change' }]
        // },
        {
          type: 'INPUT',
          label: '合格证邮寄地址',
          fieldName: 'certificateMailAddress',
          selfCols: 3,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
      ];
    },
  },
};
</script>